<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="角色名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入角色名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wvp:dataRole:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wvp:dataRole:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wvp:dataRole:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wvp:dataRole:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataRoleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="角色ID" align="center" prop="id" />
      <el-table-column label="角色名称" align="center" prop="name" />
      <el-table-column label="显示顺序" align="center" prop="sort" />
<!--      <el-table-column label="数据范围" align="center" prop="dataScope" />-->
<!--      <el-table-column label="菜单树选择项是否关联显示" align="center" prop="menuCheckStrictly" />-->
<!--      <el-table-column label="部门树选择项是否关联显示" align="center" prop="deptCheckStrictly" />-->
<!--      <el-table-column label="角色状态" align="center" prop="status" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wvp:dataRole:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wvp:dataRole:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <plugin-form ref="pluginFormRef" @close="close"></plugin-form>
  </div>
</template>

<script setup name="DataRole">
import PluginForm from './components/Form.vue'
import { listDataRole, getDataRole, delDataRole} from "@/api/wvp/dataRole";
const pluginFormRef =ref()
const { proxy } = getCurrentInstance();

const dataRoleList = ref([]);

const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);


const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    sort: null,
    dataScope: null,
    menuCheckStrictly: null,
    deptCheckStrictly: null,
    status: null,
  },
});

const { queryParams, rules } = toRefs(data);

/** 查询部门数据权限列表 */
function getList() {
  loading.value = true;
  listDataRole(queryParams.value).then(response => {
    dataRoleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}





/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/** 新增按钮操作 */
function handleAdd() {

  pluginFormRef.value.begin(false);
}


/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
    pluginFormRef.value.begin(_id)
}
// 关闭刷新页面
function close(reset =false) {
   if(reset){
       resetQuery()
   }else{
       getList()
   }
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除部门数据权限编号为"' + _ids + '"的数据项？').then(function() {
    return delDataRole(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}



/** 导出按钮操作 */
function handleExport() {
  proxy.download('wvp/dataRole/export', {
    ...queryParams.value
  }, `dataRole_${new Date().getTime()}.xlsx`)
}

getList();
</script>
